<?php
namespace Home\Controller;
use Common\Controller\MyInitController;
class TeamController extends MyInitController {
    public function index(){		
		/*=========定义变量=======*/
		$openid   = $this->openid;		
		$Teammate = D("teammate");
		$Class    = D("class");
		$Discussion  = D("discussion");
		$Random   = A('Random'); 
		$Inclass  = A('Inclass');
		$Team = D("team");
		/*======获取同组成员======*/
		
		$teamname = $this->getTeamname($openid);
					
		if($teamname){
			$teanmates = $Teammate->where("teamname = '{$teamname}' AND isdelete = 0")->field("openid")->select();						
			foreach($teanmates as $t) $class[] = $Class->where($t)->field("username,number,class")->find();
			$this->assign('class',$class);
			
			/*======获取讨论报告======*/
			$discussions = $Discussion->where("teamname = '{$teamname}'")->select();
			
			$this->assign('discussion',$discussions);
			$this->assign('discussioncount',count($discussions));
		}else{
			$teamnames = $Team->field("teamname")->select();
			$this->assign('teamnames',$teamnames);
		}

		/*======获取自身信息======*/
		$informa 	= $Class->where("openid='{$openid}'")->field("username,number,class")->find();
		$randomnum 	= $Random->getItemNum($openid); //从Random控制器调用getItemNum方法获取随机练习答题数
		$inclassnum = $Inclass->getInclassNum($openid);
		
		/*======设定模版变量======*/
		
		
		$this->assign('openid',$openid);
		$this->assign('randomnum',$randomnum);
		$this->assign('inclassnum',$inclassnum);
		$this->assign('informa',$informa);
		$this->assign('teamname',$teamname);
				
		$this->display();
    }
	public function logoutTeam(){ //退出小组
		if(IS_AJAX){
			/*=========获取变量=======*/
			$openid   = I('post.openid');
			
			$Teammate = D("teammate");
			$Teammate->isdelete = 1;
			$Teammate->logouttime = date("Y-m-d H:i:s");
			
			
			$result = $Teammate->where("openid = '{$openid}' AND isdelete = 0")->save();
			if($result) {
				$ajaxreturn = 1;
			}else{
				$ajaxreturn = '未加入小组,退出失败!';
			}
			
			/*====返回结果到前台======*/						
			$this->ajaxReturn($ajaxreturn);     			     
        }
		else $this->ajaxReturn('非法的请求方式'); 
	}
	public function createTeam(){ //创建小组
		if(IS_AJAX){
			/*=========获取变量=======*/
			$openid   = I('post.openid');
			$teamname = I('post.teamname');
			if($this->getTeamname($openid)) $this->ajaxReturn('请先退队') ;
			
			$Team = D("team");
			if($Team->create()){
				$result = $Team->add();
				if($result) {
					$ajaxreturn = 1;
				}else{
					$ajaxreturn = '非法的请求方式';
				}		
			}else{
				$ajaxreturn = '非法的请求方式';
			}
			
			/*====返回结果到前台==*/
			if(!$this->addTeammate($openid,$teamname)) $ajaxreturn = '非法的请求方式';
			
			$this->ajaxReturn($ajaxreturn);     			     
        }
		else $this->ajaxReturn('非法的请求方式'); 
	}
	public function joinTeam(){ //加入小组
		if(IS_AJAX){
			
			/*=========获取变量=======*/
			$openid   = I('post.openid');
			$teamname = I('post.teamname');
			$passwd   = I('post.passwd');
			
			
			/*======获取小组信息=======*/
			$Team = D("team");
			$tmp = $Team->find($teamname);
			$password = $tmp['passwd'];
			$ajaxreturn = ($password === $passwd)? 1:$this->ajaxReturn('口令与组名不匹配');
			
			if($this->getTeamname($openid)) $this->ajaxReturn('请先退队');
			
			if(!$this->addTeammate($openid,$teamname)) $ajaxreturn = '非法的请求方式';
			/*====返回结果到前台==*/
			$ajaxreturn['username']='1111';
			$ajaxreturn['pw']='1111';
			
			$this->ajaxReturn($ajaxreturn);     			     
        }
		else $this->ajaxReturn('非法的请求方式'); 
	}
	public function upload($savePath='/Discussion/'){ //定义默认上传路径
		$Discussion = D("discussion");
		
		$discussionNum = I('post.discussionNum');

		if($discussionNum=="") $this->error("请选择讨论报告的任务");
		
		$upload = new \Think\Upload();
		$upload->maxSize =3145728;  //以字节为单位，这里设置最大为3M
		$upload->exts =array('doc','docx');
		$upload->rootPath = './Public/Upload'; // 设置附件上传根目录

		$upload->savePath = $savePath; // 设置附件上传（子）目录 在本地创建Upload目录
		$upload->subName  = $discussionNum;
		$upload->uploadReplace = TRUE;
		$info 	= $upload->upload();
		if(!$info){			
			//return false;
			$this->error($upload->getError());
		}else{
			foreach($info as $file){
				//改变路径以移植sae
				$path = $file['savepath'].$file['savename'];
				$path = str_replace('/Upload/','',$path);
			}
		}
				
		$data["openid"] 		= 	I('post.openid');
		$data["teamname"] 		=	I('post.teamname');		
		$data["fileSaveName"] 	= 	$path;
		$data["discussionNum"]	= 	$discussionNum;
		
		if($Discussion->add($data)){
			$this->success("添加成功！");
		}
		else{
			$this->error("添加失败！");
		}
	}
	public function getTeamname($openid){
		$_Teammate 	= 	D("teammate");
		$s 			= 	$_Teammate->where("openid = '{$openid}' AND isdelete = 0")->find();
		unset($_Teammate);
		if(is_null($s)) return 0;
		else return $s['teamname'];
	}
	public function addTeammate($openid,$teamname){
		
		$_Teammate 				= 	D("teammate");
		$_Teammate->openid 		= 	$openid;
		$_Teammate->teamname 	= 	$teamname;
		$_Teammate->jointime 	= 	date("Y-m-d H:i:s");
		$result 				= 	$_Teammate->add();
		unset($_Teammate);
		return $result ;
		
	}
	
	
}




?>